package com.kyzh.core.pager.gamelist

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator
import com.gushenge.atools.util.getStatusBarHeight
import com.gushenge.atools.util.setHeight
import com.kyzh.core.R
import com.kyzh.core.activities.NoticeActivity
import com.kyzh.core.pager.home.homesearch.SearchActivity
import com.kyzh.core.adapters.v3.GameTopTabAdapter
import com.gushenge.core.base.fragment.BaseFragment

import kotlinx.android.synthetic.main.frag_game_title.*
import kotlinx.android.synthetic.main.fragment_game.*
import org.jetbrains.anko.support.v4.dip
import org.jetbrains.anko.support.v4.startActivity


class GameFragment : BaseFragment() {


    private var titles = arrayListOf<String>()
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        titles = arrayListOf(getString(R.string.game1),getString(R.string.game6),getString(R.string.game2),getString(R.string.game3),getString(R.string.game4),getString(R.string.game5))
        return inflater.inflate(R.layout.fragment_game, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {

        titleBar.setHeight(dip(44) + requireActivity().getStatusBarHeight())
        message.setOnClickListener {
            startActivity<NoticeActivity>()
        }
        search.setOnClickListener { startActivity<SearchActivity>() }
        val adapter = GameTopTabAdapter(this, titles)
        viewpager.adapter = adapter
        viewpager.currentItem = 0
        val mediator = TabLayoutMediator(tabLayout, viewpager){ tab, position->
            tab.text = titles[position]
        }
        mediator.attach()

        val textView: TextView = View.inflate(requireContext(), R.layout.tab_text, null) as TextView
        textView.text = tabLayout.getTabAt(0)?.text
        tabLayout.getTabAt(0)?.customView = textView
        tabLayout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
            override fun onTabSelected(tab: TabLayout.Tab?) {
                val textView1: TextView = View.inflate(requireContext(), R.layout.tab_text, null) as TextView
                textView1.text = tab?.text
                tab?.customView = textView1
            }

            override fun onTabUnselected(tab: TabLayout.Tab?) {
                tab?.customView = null
            }

            override fun onTabReselected(tab: TabLayout.Tab?) {
            }
        })
    }
}